package net.jast.sanitizer.impl.basic;

public class ExceptionSanitizer extends StringSanitizer {

    public ExceptionSanitizer(String... patterns) {
        super(patterns);
    }

    @Override
    public Object sanitize(Object obj) {
        if (obj instanceof Throwable)
            return parseThrowable((Throwable) obj);

        return obj;
    }

    private Throwable parseThrowable(Throwable t) {
        if (t == null)
            return null;

        final String message = doSanitize(t.getMessage());
        if (t.getCause() != null) {
            final Throwable throwable = new Throwable(message, parseThrowable(t.getCause()));
            throwable.setStackTrace(t.getStackTrace());
            return throwable;
        }

        final Throwable throwable = new Throwable(message);
        throwable.setStackTrace(t.getStackTrace());
        return throwable;
    }
}
